﻿// ***********************************************************************
// Project			: 
// Assembly         : Micua.Core
// Author           : Administrator
// Created          : 2014年01月13日 12:05
//
// Last Modified By : Administrator
// Last Modified On : 2014年01月13日 12:15
// ***********************************************************************
// <copyright file="ExceptionFilter.cs" company="Wedn.Net">
//     Copyright (c) Wedn.Net. All rights reserved.
// </copyright>
// <summary>异常筛选器</summary>
// ***********************************************************************

using System;
using System.Web.Mvc;
using Micua.Core.Tracing;
using Micua.Utility;

namespace Micua.UI.Filter
{
    /// <summary>
    /// 异常筛选器。
    /// </summary>
    /// <remarks>
    ///  2014年01月13日 12:15 Created By Administrator
    /// </remarks>
    public class ExceptionFilter : FilterAttribute, IExceptionFilter
    {
        #region 发生异常时调用 +void OnException(ExceptionContext filterContext)
        /// <summary>
        /// 在发生异常时调用。
        /// </summary>
        /// <remarks>
        ///  2014年01月13日 12:15 Created By Administrator
        /// </remarks>
        /// <param name="filterContext">筛选器上下文。</param>
        /// <exception cref="System.ArgumentNullException">filterContext</exception>
        public void OnException(ExceptionContext filterContext)
        {
            if (filterContext == null)
                throw new ArgumentNullException("filterContext");
            if (filterContext.IsChildAction || filterContext.ExceptionHandled || filterContext.HttpContext.IsCustomErrorEnabled)
                return;
            ////友好展示
            ////var areaName = filterContext.RouteData.DataTokens["area"].ToString();
            ////var controllerName = filterContext.RouteData.Values["controller"].ToString();
            ////var actionName = filterContext.RouteData.Values["action"].ToString();
            ////var model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName);
            //var result = new ViewResult
            //{
            //    ViewName = "Error",
            //    //ViewData = new ViewDataDictionary<HandleErrorInfo>(model),
            //    //TempData = filterContext.Controller.TempData
            //};
            //result.ViewBag.Error = filterContext.Exception.Message;
            //filterContext.Result = result;
            //filterContext.ExceptionHandled = true;
            //filterContext.HttpContext.Response.Clear();
            //filterContext.HttpContext.Response.StatusCode = 500;
            //filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
            //记录日志
            LogHelper.WriteErrorLog("ExceptionFilter",filterContext.Exception.Message, filterContext.Exception);
        }
        #endregion
    }
}